package com.example.administrator.baseproject;

import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;

import baseinterface.NetChangeDeal;
import butterknife.ButterKnife;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.LoadingUtils;
import utils.ToastUtil;

import static android.view.Window.FEATURE_ACTION_BAR;
import static android.view.Window.FEATURE_NO_TITLE;


public class BaseActivity extends RootActivity implements NetChangeDeal {
    public static final String TAG = "BaseActivity";
    //网络监听的回调对象
    public static NetChangeDeal netChangeDeal;
    public View loadingView, emptyView;
    public MyHandler allHandler = SingletonClass.getSingleton(MyHandler.class);

    //默认60s
    public static int timeout = 60000;
    private boolean mSystembarVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        netChangeDeal = this;
        ButterKnife.bind(this);
        initView();
        setListener();
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {        //down的时候,移除消息
                allHandler.removeMessages(HandlerConstant.NO_OPERATION);
                allHandler.removeMessages(HandlerConstant.NO_OPERATION_EXIT);
                View v = getCurrentFocus();
                if (isShouldHideKeyboard(v, event)) {
                    boolean res=hideKeyboard(v.getWindowToken());
                    if(res){
                        //隐藏了输入法，则不再分发事件
                        return true;
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP: {        //up的时候，发送延迟消息
                allHandler.sendEmptyMessageDelayed(HandlerConstant.NO_OPERATION, Constants.TIME_OUT); // 屏保
//                allHandler.sendEmptyMessage(HandlerConstant.ACTION_UP);
                break;
            }
        }
        return super.dispatchTouchEvent(event);
    }

    public void initView() {
//        BaseActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    protected void setListener() {
    }

    public void showLoadingView(ViewGroup viewGroup) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,BaseActivity.this, "正在加载");
        }
    }

    protected void showLoadingView(ViewGroup viewGroup,String msg) {
        if (loadingView == null) {
            loadingView = LoadingUtils.showLoadingView(viewGroup,BaseActivity.this, msg);
        }
    }

    protected void hideLoadingView() {
        LoadingUtils.hideLoadingView(loadingView);
        loadingView = null;
    }

    protected void showEmptyView(ViewGroup viewGroup) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup, BaseActivity.this, "暂无数据", R.mipmap.nomub);
        }
    }

    protected void showEmptyView(ViewGroup viewGroup, String msg, int imgRes) {
        if (emptyView == null) {
            emptyView = LoadingUtils.showEmptyView(viewGroup, BaseActivity.this, msg, imgRes);
        }
    }

    protected void hideEmptyView() {
        LoadingUtils.hideEmptyView(emptyView);
        emptyView = null;
    }

    @Override
    public void onNetChange(int netState) {
        if (netState == Constants.NETWORK_NONE) {
            sendEmptyMessage(HandlerConstant.NETWORK_NONE);
            ToastUtil.showToast(this, "没有网络连接");
        } else if (netState == Constants.NETWORK_MOBILE) {
            sendEmptyMessage(HandlerConstant.NETWORK_MOBILE);
            ToastUtil.showToast(this, "移动网络已连接");
        } else if (netState == Constants.NETWORK_WIFI) {
            sendEmptyMessage(HandlerConstant.NETWORK_WIFI);
            ToastUtil.showToast(this, "WIFI已连接");
        } else if (netState == Constants.NETWORK_ETHERNET) {
            sendEmptyMessage(HandlerConstant.NETWORK_ETHERNET);
            ToastUtil.showToast(this, "以太网已连接");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        BaseActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }


    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     * @param token
     */
    private boolean hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            return  im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
        return false;
    }


}
